package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public Travel selectContainContentByTravelId(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContentMapper.selectById(id));
        return travel;
    }

    @Override
    public AjaxResult saveOrUpdate(Travel travel) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //设置默认属性
            travel.setLastUpdateTime(new Date());
            travel.setSummary("人工,穷");
            //这里假装已经审核通过了
            travel.setState(2);
            if (travel.getId() == null){
                travel.setCreateTime(new Date());
                travelMapper.insert(travel);
                travelContentMapper.insert(travel.getId(), travel.getTravelContent().getContent());
            }else{
                travelMapper.updateByPrimaryKey(travel);
                travelContentMapper.update(travel.getId(), travel.getTravelContent().getContent());
            }
            ajaxResult.addData(travel.getId());
        }catch (Exception e){
            ajaxResult.mark(e.getMessage());
        }
        return ajaxResult;
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

}
